/*
 * @Author: your name
 * @Date: 2021-03-29 17:48:43
 * @LastEditTime: 2021-03-29 17:48:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \active-front\src\utils\tool.js
 */
//下载
import moment from "moment";

export const fileSaver = (data, downloadName = "下载文件", mime) => {
  if (data instanceof Blob) {
    data = window.URL.createObjectURL(data);
  }
  let downloadFile = document.createElement("a");
  downloadFile.href = data;
  let dateTimeStr = moment().format("YYYY-MM-DD-HH-mm-ss");
  downloadFile.setAttribute(
    "download",
    `${downloadName}-${dateTimeStr}.${mime}`
  );
  downloadFile.click();
};

export const download = (url, filename) => {
  return fetch(url).then(res =>
    res.blob().then(blob => {
      let a = document.createElement("a");
      let url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = filename;
      a.click();
      window.URL.revokeObjectURL(url);
    })
  );
};
